package com.pet.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.service.SysPetTypeService;
import com.pet.system.SysPetType;
import com.pet.utils.Result;
import com.pet.vo.SysPetTypeQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 宠物类型表 前端控制器
 * </p>
 *
 * @author whd
 * @since 2023-08-01
 */
@Api(tags = "宠物类型表前端控制器")
@RestController

@RequestMapping("/lovePet/system/sysPetType")
@CrossOrigin("*")
public class SysPetTypeController {


    @Autowired
    private SysPetTypeService sysPetTypeService;

    // 分页和条件查询

    /**
     * @param page
     * @param limit
     * @param sysPetTypeQueryVo
     * @return
     */
    @ApiOperation("分页和查询")
    @PostMapping("/{page}/{limit}")
    public Result findCategoryByPageQuery(@ApiParam("页码") @PathVariable Long page,
                                          @ApiParam("每页条数") @PathVariable Long limit,
                                          @ApiParam("包装的查询分类名称名称") @RequestBody SysPetTypeQueryVo sysPetTypeQueryVo) {

        System.out.println("========>" + sysPetTypeQueryVo.getName());

        //1.创建分页对象
        IPage<SysPetType> iPage = new Page<SysPetType>(page, limit);
        //2.调用方法
        iPage = this.sysPetTypeService.selectPage(iPage, sysPetTypeQueryVo);
        //3.返回
        return iPage == null ? Result.fail() : Result.ok(iPage);
    }


    /**
     * @return
     */
    @ApiOperation("获取所有宠物类型")
    @GetMapping("/findAll")
    public Result findAllPet() {
        List<SysPetType> list = sysPetTypeService.list();
        return Result.ok(list);
    }

    /**
     *
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除宠物类型")
    @DeleteMapping("/removePetTypeByIds")
    public Result removePetTypeByIds(@ApiParam("待删除的宠物类型ID列表") @RequestBody List<Long> ids) {
        boolean b = this.sysPetTypeService.removeBatchByIds(ids);
        return b ? Result.ok() : Result.fail();
    }


    /**
     * 添加宠物类别
     *
     * @param sysPetType
     * @return
     */
    @ApiOperation("添加宠物类别")
    @PostMapping("/addPetType")
    public Result addPetType(@ApiParam("宠物类型信息") @RequestBody SysPetType sysPetType) {
        boolean b = this.sysPetTypeService.save(sysPetType);
        return b ? Result.ok() : Result.fail();
    }


    /**
     *
     * 1.根据id 去得到当前分类
     *
     * @param id
     * @return
     */
    @ApiOperation("根据ID获取宠物类型")
    @GetMapping("/findPetTypeById/{id}")
    public Result findPetTypeById(@ApiParam("宠物类型ID") @PathVariable Long id) {
       SysPetType sysPetType = this.sysPetTypeService.getById(id);
        return Result.ok(sysPetType);
    }


    /**实现修改
     * @param sysPetType
     * @return
     */
    @ApiOperation("更新宠物类型")
    @PostMapping("/updatePetTypeById")
    public Result updatePetTypeById(@ApiParam("宠物类型信息") @RequestBody SysPetType sysPetType) {
        boolean b = this.sysPetTypeService.updateById(sysPetType);
        return b ? Result.ok() : Result.fail();
    }


    /**删除
     * @param id
     * @return
     */
    @ApiOperation("根据ID删除宠物类型")
    @DeleteMapping("/removePetTypeById/{id}")
    public Result removePetTypeById(@ApiParam("宠物类型ID") @PathVariable Long id) {
       boolean b = this.sysPetTypeService.removeById(id);
        return b ? Result.ok() : Result.fail();

    }


}

